/*
	@Author：TangYi

	Description:一个通用的Windows C语言的轻型网络框架
	风格说明：虽然这是一个Windows程序，但试用linux的命名风格.

*/

#ifndef _CONNECTION_H_
#define _CONNECTION_H_

#include <windows.h>
#include "hash.h"
#include "packet.h"
#include "session.h"
#include "circular_buffer.h"

struct connection_s
{
	SOCKET con_socket;
	session_t* session;
	packet_t* receving_packet;
	circular_buffer_t* obuf;
};

typedef connection_s connection_t;

hashtable_t* init_con();
connection_t* add_new_connection(SOCKET socket);
connection_t* get_connection(SOCKET socket);

//读取套接字，并进行分包
//返回：
//	0	成功，读取完整分包
//	1	失败，没有读取完整分包，下次接着读
//	2	失败，网络断开连接
//	3	失败，超过最大包长度
//
int read_packet(connection_t* con,packet_t** packet);
void handle_new_connection(connection_t* con);
void handle_disconnect(connection_t* con);
void handle_packet(connection_t* con,packet_t* packet);

int delete_connection(SOCKET socket);
int delete_connection(connection_t* con);

int set_session(connection_t* con,char* session_name,void* value,
				pfunc_value_dispose_callback* dispose_callback);
int get_session(connection_t* con,char* session_name,void** value);
int delete_session(connection_t* con,char* session_name);

//从接收队列中POP一个PACKET
packet_t* pop_back_connection_recv_packet(connection_t* con);
//向发送队列中PUSH一个PACKET
void push_front_connection_send_packet(connection_t* con,packet_t* packet);


int connection_on_exceptions(connection_t* con);
int connection_on_write(connection_t* con);
int connection_on_read(connection_t* con);

void flush_packets_to_output_buffer(connection_t* con);
void flush_all_connections_packets();

#endif